<template>
  <view class="home">
    <!-- 搜索栏，uniapp中建好后直接引用就好 -->
      <div class="searchBox">
         <my_search @click.native="goSearch"></my_search>
      </div>
    <!-- 轮播图 -->
    <swiper class="swiper" circular :indicator-dots="true" autoplay duration="800" interval="3000"
      indicator-active-color="white">
      <navigator v-for="(item,i) in swiperList" :key="item.id"
        :url="'../../subpkg/goods_detail/goods_detail?goods_id='+item.goods_id">
        <swiper-item>
          <image :src="item.image_src"></image>
        </swiper-item>
      </navigator>
    </swiper>
    <!-- 分类导航 -->
    <view class="cate">
      <view class="item" v-for="(item,index) in cateList" :key="index" @click="cateHandler(item)">
        <image :src="item.image_src"></image>
      </view>
    </view>
    <!-- 楼层区域 -->
    <view class="floor">
      <view class="floor_item" v-for="(item,i) in floorList" :key="i">
        <view class="title">
          <image :src="item.floor_title.image_src" mode="aspectFit" class="floor_title"></image>
        </view>
        <view class="product">
          <view class="left">
            <image :src="item.product_list[0].image_src" :style="{width:item.product_list[0].image_width+'rpx'}"
              mode="widthFix" @click="toGoodsList(item.product_list[0].product_url)"></image>
          </view>
          <view class="right">
            <block v-for="(itemChild,i2) in item.product_list" :key="i2" v-if="i2!==0">
              <image :src="itemChild.image_src" :style="{width:itemChild.image_width+'rpx'}" mode="widthFix"
                @click="toGoodsList(itemChild.product_url)"></image>
            </block>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
 import cartBadge from '@/mixins/tabbar-badge.js'
 export default {
   mixins:[cartBadge],
    data() {
      return {
        swiperList: [], //轮播图数据
        cateList: [], //分类导航数据
        floorList: [], //楼层数据
      }
    },
    // created()() {
    //   this.getSwiperList();都可以
    // },
    onLoad() {
      this.getSwiperList();
      this.getCateList()
      this.getFloorList()
    },
    methods: {
      // 获取轮播图数据
      async getSwiperList() {
        const {
          data
        } = await uni.$http.get('/api/public/v1/home/swiperdata');
        // console.log(data);
        if (data.meta.status !== 200) return uni.$showMsg()
        this.swiperList = data.message
      },
      // 获取分类导航数据
      async getCateList() {
        const {
          data
        } = await uni.$http.get('/api/public/v1/home/catitems');
        if (data.meta.status !== 200) return uni.$showMsg()
        this.cateList = data.message
      },
      // 点击分类导航切换页面
      cateHandler(item) {
        if (item.name === '分类') {
          uni.switchTab({
            url: "/pages/cate/cate"
          })
        }
      },
      // 获取楼层数据
      async getFloorList() {
        const {
          data
        } = await uni.$http.get('/api/public/v1/home/floordata');
        if (data.meta.status !== 200) return uni.$showMsg()
        data.message.forEach(item => {
          item.product_list.forEach(item2 => {
            item2.product_url = '/subpkg/goods_list/goods_list?' + item2.navigator_url.split('?')[1];
          })
        })
        this.floorList = data.message
      },
      // 点击楼层产品图片跳转
      toGoodsList(toUrl){
        //在起始页面跳转到test.vue页面并传递参数
        uni.navigateTo({
        	url:toUrl
        });
      },
      // 点击搜索跳转到搜索页面
        goSearch(){
        uni.navigateTo({
          url:"/subpkg/search/search"
        })
        },
    }
  }
</script>

<style lang="scss" scoped>
  .searchBox{
    position: sticky;
    top: 0;
    z-index: 999;
  }
  .swiper {
    height: 330rpx;

    image {
      width: 100%;
      height: 100%;
    }
  }

  .cate {
    display: flex;
    justify-content: space-around;
    margin: 20rpx 0;

    .item {
      flex: 1;
      display: flex;
      justify-content: center;
      align-items: center;

      image {
        width: 80%;
        height: 140rpx;
      }
    }
  }

  .floor_title {
    height: 60rpx;
    width: 100%;
  }

  .product {
    display: flex;
    padding-left: 10rpx;

    .right {
      display: flex;
      justify-content: space-around;
      flex-wrap: wrap;
    }
  }
</style>
